<template>
  <aside class="sidebar">
    <!-- 个人信息卡片 -->
    <div class="profile-card">
      <div class="profile-header">
        <img src="@/assets/images/avatar.webp" alt="头像" class="profile-avatar">
        <h3 class="profile-name">lzy</h3>
      </div>
      <p class="profile-bio">昱神天下无双</p>
      
      <div class="profile-stats">
        <div class="stat-item">
          <span class="stat-number">{{ sidebarData.articleCount || 0 }}</span>
          <span class="stat-label">文章数</span>
        </div>
        <div class="stat-item">
          <span class="stat-number">{{ sidebarData.categoryCount || 0 }}</span>
          <span class="stat-label">分类</span>
        </div>
        <div class="stat-item">
          <span class="stat-number">{{ sidebarData.tagCount || 0 }}</span>
          <span class="stat-label">标签</span>
        </div>
        <div class="stat-item">
          <span class="stat-number">{{ sidebarData.visitCount || 0 }}</span>
          <span class="stat-label">访问量</span>
        </div>
      </div>
      
      <div class="social-links">
        <a href="https://github.com" 
           target="_blank" 
           class="social-link"
           title="GitHub">
          <img src="@/assets/icons/GitHub.svg" alt="GitHub">
        </a>
        
        <!-- Twitter -->
        <a href="https://gitee.com" 
           target="_blank" 
           class="social-link"
           title="Twitter">
          <img src="@/assets/icons/Gitee.svg" alt="Gitee">
        </a>
        
        <!-- 微信 -->
        <a href="javascript:void(0);" 
           class="social-link" 
           @click="copyWeChat"
           :title="weChatTooltip">
          <img src="@/assets/icons/WeChat.svg" alt="WeChat">
          <span class="tooltip" v-if="showWeChatTooltip">点击复制</span>
        </a>
        
        <!-- 邮箱 -->
        <a href="javascript:void(0);" 
           class="social-link" 
           @click="copyQQ"
           :title="QQTooltip">
          <img src="@/assets/icons/QQ.svg" alt="QQ">
          <span class="tooltip" v-if="showQQTooltip">点击复制</span>
        </a>
      </div>
    </div>

    <!-- 内容分类区域 -->
    <div class="content-section">
      <!-- 分类 -->
      <div class="category-widget">
        <h3 class="section-title">分类</h3>
        <div class="section-content scrollable-container">
          <CategoryList :categories="processedCategories" />
        </div>
      </div>
      
      <!-- 标签云 -->
      <div class="tag-widget">
        <h3 class="section-title">标签云</h3>
        <div class="section-content">
          <TagCloud 
            :tags="sidebarData.tagList.map(tag => ({
              tagId: tag.id,
              tagName: tag.tagName
            }))" 
          />
        </div>
      </div>
    </div>
  </aside>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { useToast } from 'vue-toastification'
import CategoryList from '@/components/blog/CategoryList.vue'
import TagCloud from '@/components/common/tagCloud.vue'
import { getSidebar } from '@/api/article'

const toast = useToast()

// 侧边栏数据
const sidebarData = ref({
  articleCount: 0,
  categoryCount: 0,
  tagCount: 0,
  visitCount: 0,
  categoryDetailVOlist: [],
  tagList: []
})

// 处理分类数据，适配CategoryList组件
const processedCategories = computed(() => {
  return sidebarData.value.categoryDetailVOlist?.map(item => ({
    categoryId: item.category?.id || 0,
    categoryName: item.category?.categoryName || '',
    articleCount: item.categoryCount || 0
  })) || []
})

// 微信相关状态
const showWeChatTooltip = ref(true)
const weChatTooltip = ref('点击复制微信号')
const weChatText = 'hp18407920387'

// 邮箱相关状态
const showQQTooltip = ref(true)
const QQTooltip = ref('点击复制邮箱')
const emailText = '2893989810'

// 加载数据
const loadData = async () => {
  try {
    const res = await getSidebar()
    sidebarData.value = res.data
  } catch (error) {
    console.error('加载侧边栏数据失败:', error)
    toast.error('加载数据失败，请稍后重试')
  }
}

// 组件挂载时加载数据
onMounted(() => {
  loadData()
})

const copyToClipboard = async (text: string, type: string) => {
  try {
    await navigator.clipboard.writeText(text)
    
    if (type === '微信') {
      showWeChatTooltip.value = false
      weChatTooltip.value = '已复制'
      setTimeout(() => {
        showWeChatTooltip.value = true
        weChatTooltip.value = '点击复制微信号'
      }, 2000)
    } else {
      showQQTooltip.value = false
      QQTooltip.value = '已复制'
      setTimeout(() => {
        showQQTooltip.value = true
        QQTooltip.value = '点击复制QQ'
      }, 2000)
    }
    
    toast.success(`${type}已复制: ${text}`, {
      timeout: 2000,
      icon: 'fas fa-copy'
    })
  } catch (err) {
    console.error('复制失败:', err)
    const textarea = document.createElement('textarea')
    textarea.value = text
    document.body.appendChild(textarea)
    textarea.select()
    document.execCommand('copy')
    document.body.removeChild(textarea)
    
    toast.success(`${type}已复制: ${text}`, {
      timeout: 2000,
      icon: 'fas fa-copy'
    })
  }
}

const copyWeChat = () => copyToClipboard(weChatText, '微信')
const copyQQ = () => copyToClipboard(emailText, 'QQ')
</script>

<style scoped>
/* 保持原有样式不变 */
.sidebar {
  width: 330px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  background: transparent;
  box-shadow: none;
  padding: 0;
  position: static;
  top: 20px;
  align-self: flex-start;
}

.profile-card {
  background: white;
  padding: 1.5rem;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
  text-align: center;
  margin-left: 15px;
}

.content-section {
  display: flex;
  flex-direction: column;
  gap: 20px;
  margin-left: 15px;
}

.profile-header {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 1rem;
}

.profile-avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  object-fit: cover;
  margin-right: 1rem;
  border: 3px solid #f0f0f0;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}

.profile-name {
  margin: 0;
  font-size: 1.4rem;
  color: #333;
  font-weight: 600;
}

.profile-bio {
  margin: 0 0 1.5rem;
  color: #666;
  font-size: 0.95rem;
  line-height: 1.5;
  font-style: italic;
}

.profile-stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1rem;
  margin-bottom: 1.5rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid #eee;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stat-number {
  font-weight: 700;
  font-size: 1.2rem;
  color: #2c3e50;
  margin-bottom: 0.2rem;
}

.stat-label {
  font-size: 0.8rem;
  color: #7f8c8d;
}

.social-links {
  display: flex;
  justify-content: center;
  gap: 1.5rem;
  padding-top: 1rem;
}

.social-link {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: #f5f5f5;
  transition: all 0.3s ease;
  position: relative;
}

.social-link:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.social-link:active {
  transform: scale(0.95);
}

.social-link img {
  width: 20px;
  height: 20px;
}

.tooltip {
  position: absolute;
  top: -30px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 4px 10px;
  border-radius: 4px;
  font-size: 12px;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s;
}

.social-link:hover .tooltip {
  opacity: 1;
}

.category-widget,
.tag-widget {
  background: white;
  padding: 1.5rem;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
  height: 380px;
  display: flex;
  flex-direction: column;
}

.section-title {
  margin-top: 0;
  margin-bottom: 1rem;
  font-size: 1.2rem;
  color: #2c3e50;
  font-weight: 600;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid #eee;
}

.section-content {
  color: #666;
  font-size: 0.95rem;
  line-height: 1.6;
  flex: 1;
}

.scrollable-container {
  overflow-y: auto;
  padding-right: 8px;
  margin-right: -8px;
}

.scrollable-container::-webkit-scrollbar {
  width: 6px;
}

.scrollable-container::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.scrollable-container::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

.scrollable-container::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

:deep(.category-list) {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

:deep(.category-item) {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 15px;
  background: white;
  border-radius: 6px;
  transition: all 0.2s ease;
}

:deep(.category-item:hover) {
  background: #e9ecef;
  transform: translateX(3px);
}

:deep(.category-name) {
  font-size: 0.95rem;
  color: #495057;
  font-weight: 500;
}

:deep(.category-count) {
  font-size: 0.85rem;
  color: #6c757d;
  background: #e9ecef;
  padding: 2px 8px;
  border-radius: 10px;
}

.tag-cloud {
  display: flex;
  flex-wrap: wrap;
  gap: 0.8rem;
  margin-top: 10px;
}

.tag-widget .tag {
  display: inline-block;
  padding: 0.4rem 0.8rem;
  background: #f0f7ff;
  border-radius: 15px;
  color: #1890ff;
  text-decoration: none;
  transition: all 0.2s ease;
}

.tag-widget .tag:hover {
  background: #1890ff;
  color: white;
  transform: translateY(-2px);
}
</style>